package week8;

import java.util.*;

/**
 * A simple textual interface example exercising a Rectangle.
 */
public class MainRectangle {
  
  public static void main (String[] argv) {
    (new RectangleTUI()).start();
  }
}

/**
 * A simple text-based user interface for creating and exercising a Rectangle.
 */
class RectangleTUI {
 
  // User menu choices:
  private static final int NO_CHOICE = 0;
  private static final int LENGTH = 1;
  private static final int WIDTH = 2;
  private static final int AREA = 3;
  private static final int PERIMETER = 4;
  private static final int NEW = 5;
  private static final int EXIT = 6;
  
  private RectangleArray rectangles;
  private Scanner in; 

  /**
   * Create a user interface.
   */
  public RectangleTUI () {
    this.rectangles = null;
    this.in = new Scanner(System.in);
  }

  /**
   * Run the user interface.
   */
  public void start () {
    createRectangle();
    int choice = NO_CHOICE;
    while (choice != EXIT) {
      displayMenu();
      choice = readIntWithPrompt("Enter choice: ");
      executeChoice(choice);
    }
  }

  /**
   * Create a Rectangle with dimensions provided by the user.
   */
  private void createRectangle () {
    int length = -1;
    int width = -1;
    while (length < 0)
      length = readIntWithPrompt("Rectangle length (a non-negative integer): ");
    while (width < 0)
      width = readIntWithPrompt("Rectangle width (a non-negative integer): ");
    this.rectangles = new RectangleArray( new Rectangle(length,width) );
  }
 
  /**
   * Display top level menu.
   */
  private void displayMenu () {
    System.out.println();
    System.out.println("Enter the number denoting the action " +
                       "to perform:");
    System.out.println( "Display length.............." + LENGTH);
    System.out.println("Display width..............." + WIDTH);
    System.out.println("Display area................" + AREA);
    System.out.println("Display perimeter..........." + PERIMETER);
    System.out.println("Create new rectangle........" + NEW);
    System.out.println("Exit........................" + EXIT);
  }

  /**
   * Execute user's menu choice.
   */
  private void executeChoice (int choice) {
    System.out.println();
    if (choice == LENGTH)
      System.out.println("Length is " + rectangles.get().length());
    else if (choice == WIDTH)
      System.out.println("Width is " + rectangles.get().width());
    else if (choice == AREA)
      System.out.println("Area is " + rectangles.get().area());
    else if (choice == PERIMETER)
      System.out.println("Perimeter is " + rectangles.get().perimeter());
    else if (choice == NEW)
      createRectangle();
    else if (choice == EXIT)
      System.out.println("Goodbye.");
  }
  
  /**
   * Read an int from system in with the speciifed prompt.
   */
  private int readIntWithPrompt (String prompt) {
    System.out.print(prompt); System.out.flush();
    while (!in.hasNextInt()) {
      in.nextLine();
      System.out.print(prompt); System.out.flush();
    }
    int input = in.nextInt();
    in.nextLine();
    return input;
  }  
}
